package com.dooioo.pns.api.v2.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.dooioo.pns.business.service.MessageReadService;
import com.dooioo.pns.business.service.MessageService;
import com.dooioo.pns.business.service.v2.BusinessServiceV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * PackageName:com.dooioo.pns.api.controller
 * Author: Jerry.hu
 * Create: Jerry.hu (2015-05-13 下午6:46)
 * Description:
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/api/v2/message")
public class MessageControllerV2 {
	@Autowired
	private MessageService messageService;
	@Autowired
	private MessageReadService messageReadService;
	@Autowired
	private BusinessServiceV2 businessServiceV2;


	/**
	 * 消息详情列表
	 * @param userCode 用户工号
	 * @param appId app 应用Id
	 * @param businessId 业务推送方id
	 * @return list
	 */
	@RequestMapping(value = "/{appId}/{userCode}/{businessId}",method = RequestMethod.GET)
	public ResponseEntity<?> list(@PathVariable int userCode,@PathVariable int appId,@PathVariable int businessId,
								 @RequestParam(value = "pageSize",defaultValue = "20")int pageSize,
								 @RequestParam(value = "pageNo",defaultValue = "1")int pageNo){
		return ResponseEntity.ok(messageService.getMessagePagination(userCode,appId,businessId,pageSize,pageNo));
	}

	/**
	 * 消息分类列表
	 * @param userCode 用户工号
	 * @param appId app 应用Id
	 * @return list
	 */
	@RequestMapping(value = "/{userCode}/{appId}",method = RequestMethod.GET)
	public ResponseEntity<?> businessList(@PathVariable int userCode,@PathVariable int appId){
		return ResponseEntity.ok(businessServiceV2.getBusinessList(userCode,appId));
	}

	/**
	 * 更新消息已读
	 * @param userCode 工号
	 * @param messageIds
	 * @return
	 */
	@RequestMapping(value = "/{userCode}/read",method = RequestMethod.PUT)
	public ResponseEntity<?> read(@PathVariable int userCode,
	                              @RequestParam(value = "messageIds",required = true)String messageIds){
		JSONArray messageIdArray;
		Map<String,String> result = new HashMap<>();
		try {
			messageIdArray = JSON.parseArray(messageIds);
		}catch (Exception e){
			result.put("code","400");
			result.put("message","消息id JSONArray 格式不对");
			return new ResponseEntity<Object>(result, HttpStatus.BAD_REQUEST);
		}

		if(messageReadService.readMessage(userCode,messageIdArray)){
				result.put("message","注销成功");
				result.put("code","0");
		}else {
			result.put("code","0");
			result.put("message","设置已读失败");
		}
		return  ResponseEntity.ok(result);
	}

	/**
	 * 获取员工的消息的未读条数
	 * @param userCode
	 * @return
	 */
	@RequestMapping(value = "/{userCode}/unread",method = RequestMethod.GET)
	protected ResponseEntity<?> getUnread(@PathVariable int userCode){
		Map<String,Integer> map = new HashMap<>();
		map.put("count",messageReadService.getUnreadCount(userCode,1000));
		return ResponseEntity.ok(map);
	}

}
